package com.xhh.enjoyapp.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xhh.enjoyapp.Bean.Post;
import com.xhh.enjoyapp.Bean.User;
import com.xhh.enjoyapp.R;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-15 11:12
 **/
public class PushContent extends AppCompatActivity {

    private EditText pushContent;
    private ImageView back;
    private Button push;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pushcontent);

        initView();

        // 发帖子
        push.setOnClickListener(click->{
            if(pushContent.getText().toString().trim().equals("")){
                Toast.makeText(PushContent.this, "请输入内容", Toast.LENGTH_SHORT).show();
            } else {
                User user = BmobUser.getCurrentUser(User.class);
                Post po = new Post();
                po.setName(user.getUsername());
                po.setContent(pushContent.getText().toString().trim());
                po.setAuthor(user);
                po.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if(e == null){
                            pushContent.setText("");
                            Toast.makeText(PushContent.this, "发布成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(PushContent.this, "发布失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

        back.setOnClickListener(click->{
            finish();
        });
    }

    private void initView() {
        pushContent = findViewById(R.id.push_content_et);
        push = findViewById(R.id.push_content_btn);
        back = findViewById(R.id.push_content_back);
    }

}
